﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;

namespace HenIT.CAGCompare
{
    public class HTMLReportWriter
    {
        private StringBuilder sbContent = new StringBuilder();
        private StringBuilder sbStylesheet = new StringBuilder();
        private string title = "Report";

        private bool showTableBorder;
        public bool ShowTableBorder
        {
            get { return showTableBorder; }
            set { showTableBorder = value; }
        }
        public string LastError { get; private set; }

        #region Constructors
        public HTMLReportWriter() { }

        public HTMLReportWriter(string title)
        {
            this.title = title;
        } 
        #endregion

        public bool WriteDocument(string outputFileName)
        {
            bool success = false;
            LastError = "";
            try
            {
                File.WriteAllText(outputFileName, CreateHTMLDocument(), Encoding.UTF8);
                success = true;
            }
            catch (Exception ex)
            {
                LastError = ex.ToString();
            }
            return success;
        }

        #region Add headings
        public void AddHeading1(string heading)
        {
            sbContent.AppendLine("<h1>" + heading + "</h1>");
        }
        public void AddHeading2(string heading)
        {
            sbContent.AppendLine("<h2>" + heading + "</h2>");
        }
        public void AddHeading3(string heading)
        {
            sbContent.AppendLine("<h3>" + heading + "</h3>");
        }
        #endregion

        #region AddParagraph
        public void AddParagraph(string text)
        {
            sbContent.AppendLine("<p>" + text + "</p>");
        }
        public void AddParagraph(string text, string className)
        {
            sbContent.AppendLine("<p" + (className.Length > 0? " class=\"" + className + "\"": "")  + ">" + text + "</p>");
        }
        #endregion
        public void AddHorisontalLine()
        {
            sbContent.AppendLine("<hr />");
        }

        #region AddTable
        public void AddTable(DataTable table)
        {
            sbContent.AppendLine("<table border=\"" + (showTableBorder ? "1" : "0") + "\">");
            sbContent.AppendLine("<tr>");
            foreach (DataColumn col in table.Columns)
            {
                sbContent.Append("<th>");
                sbContent.Append(col.ColumnName);
                sbContent.AppendLine("&nbsp;</th>");
            }
            sbContent.AppendLine("</tr>");

            foreach (DataRow row in table.Rows)
            {
                sbContent.AppendLine("<tr>");

                foreach (object dataValue in row.ItemArray)
                {
                    sbContent.Append("<td>");
                    sbContent.Append(dataValue.ToString());
                    if (!dataValue.ToString().EndsWith(">"))
                        sbContent.Append("&nbsp;");
                    sbContent.AppendLine("</td>");
                }
                sbContent.AppendLine("</tr>");
            }
            sbContent.AppendLine("</table>");
        }
        public void AddTableWithClass(DataTable table)
        {
            sbContent.AppendLine("<table border=\"" + (showTableBorder ? "1" : "0") + "\">");
            sbContent.AppendLine("<tr>");
            for (int i = 0; i < table.Columns.Count - 1; i++)
            {
                DataColumn col = table.Columns[i];
                sbContent.Append("<th>");
                sbContent.Append(col.ColumnName);
                sbContent.AppendLine("&nbsp;</th>");
            }
            sbContent.AppendLine("</tr>");

            foreach (DataRow row in table.Rows)
            {
                sbContent.AppendLine("<tr>");
                for (int i = 0; i < row.ItemArray.Length - 1; i++)
                {
                    string dataValue = row[i].ToString();
                    string className = row[row.ItemArray.Length - 1].ToString();
                    if (className.Length > 0)
                        sbContent.Append("<td class=\"" + className + "\">");
                    else
                        sbContent.Append("<td>");
                    sbContent.Append(dataValue);
                    if (!dataValue.EndsWith(">"))
                        sbContent.Append("&nbsp;");
                    sbContent.AppendLine("</td>");
                }
                sbContent.AppendLine("</tr>");
            }
            sbContent.AppendLine("</table>");
        }
        #endregion

        #region AddStyle
        public void AddStyle(string styleName, string styleDescription)
        {
            sbStylesheet.AppendLine(styleName + "\r\n{" + styleDescription + "}");
        }
        #endregion

        private string CreateHTMLDocument()
        {
            StringBuilder sbHeader = new StringBuilder();
            sbHeader.AppendLine("<html>");
            sbHeader.AppendLine("<head>");
            sbHeader.AppendLine("<title>" + title + "</title>");
            sbHeader.AppendLine("<style type=\"text/css\">");
            sbHeader.AppendLine(sbStylesheet.ToString());
            sbHeader.AppendLine("</style>");
            sbHeader.AppendLine("</head>");
            sbHeader.AppendLine("<body>");
            sbHeader.AppendLine(sbContent.ToString());
            sbHeader.AppendLine("</body>");
            sbHeader.AppendLine("</html>");
            return sbHeader.ToString();
        }
    }
}
